import { Dimensions } from "react-native";

import { gameDifficulty , touch1 , touch2 , touch3 , touch4 , touch5 } from '../GamePage'

export let score = 0

export function Loop(entities) {
  const block = [entities.block1,entities.block2,entities.block3,entities.block4,entities.block5]
  let selectionBlock = Math.ceil(Math.random()*10) % 5

  //物体移动速度
  const acceleration = gameDifficulty

  //block1
  //当block1正在移动
  if(block[0].position[1] > -Dimensions.get("window").height/3){
    block[0].position[1] += acceleration
  }
  //当block1已完全移动出屏幕
  if(block[0].position[1] >= Dimensions.get("window").height){
    block[0].position[1] = -Dimensions.get("window").height/3
  }
  //当block1完全露出屏幕
  if(block[0].position[1] >= 0 && block[0].position[1] <= acceleration){
    while(selectionBlock === 0 || block[selectionBlock].position[1] > -Dimensions.get("window").height/3){
      selectionBlock = (selectionBlock+2)%5
    }
    block[selectionBlock].position[1] += 0.001
  }

  //block2
  //当block2正在移动
  if(block[1].position[1] > -Dimensions.get("window").height/3){
    block[1].position[1] += acceleration
  }
  //当block2已完全移动出屏幕
  if(block[1].position[1] >= Dimensions.get("window").height){
    block[1].position[1] = -Dimensions.get("window").height/3
  }
  //当block2完全露出屏幕
  if(block[1].position[1] >= 0 && block[1].position[1] <= acceleration){
    while(selectionBlock === 1 || block[selectionBlock].position[1] > -Dimensions.get("window").height/3){
      selectionBlock = (selectionBlock+2)%5
    }
    block[selectionBlock].position[1] += 0.001
  }

  //block3
  //当block3正在移动
  if(block[2].position[1] > -Dimensions.get("window").height/3){
    block[2].position[1] += acceleration
  }
  //当block3已完全移动出屏幕
  if(block[2].position[1] >= Dimensions.get("window").height){
    block[2].position[1] = -Dimensions.get("window").height/3
  }
  //当block3完全露出屏幕
  if(block[2].position[1] >= 0 && block[2].position[1] <= acceleration){
    while(selectionBlock === 2 || block[selectionBlock].position[1] > -Dimensions.get("window").height/3){
      selectionBlock = (selectionBlock+2)%5
    }
    block[selectionBlock].position[1] += 0.001
  }

  //block4
  //当block4正在移动
  if(block[3].position[1] > -Dimensions.get("window").height/3){
    block[3].position[1] += acceleration
  }
  //当block4已完全移动出屏幕
  if(block[3].position[1] >= Dimensions.get("window").height){
    block[3].position[1] = -Dimensions.get("window").height/3
  }
  //当block4完全露出屏幕
  if(block[3].position[1] >= 0 && block[3].position[1] <= acceleration){
    while(selectionBlock === 3 || block[selectionBlock].position[1] > -Dimensions.get("window").height/3){
      selectionBlock = (selectionBlock+2)%5
    }
    block[selectionBlock].position[1] += 0.001
  }

  //block5
  //当block5正在移动
  if(block[4].position[1] > -Dimensions.get("window").height/3){
    block[4].position[1] += acceleration
  }
  //当block5已完全移动出屏幕
  if(block[4].position[1] >= Dimensions.get("window").height){
    block[4].position[1] = -Dimensions.get("window").height/3
  }
  //当block5完全露出屏幕
  if(block[4].position[1] >= 0 && block[4].position[1] <= acceleration){
    while(selectionBlock === 4 || block[selectionBlock].position[1] > -Dimensions.get("window").height/3){
      selectionBlock = (selectionBlock+2)%5
    }
    block[selectionBlock].position[1] += 0.001
  }

  //触控得分逻辑
  const touch = [touch1,touch2,touch3,touch4,touch5]

  if(touch[0] === true){
    if(block[0].position[1] <= (Dimensions.get("window").height/3*2+Dimensions.get("window").height/3)
    && block[0].position[1] >= (Dimensions.get("window").height/3*2+Dimensions.get("window").height/5-Dimensions.get("window").height/3)){

      block[0].score += 10
      score = block[0].score

    }else{
      if(block[0].score>0){
        block[0].score -= 5
        score = block[0].score
      }
    }
  }
  if(touch[1] === true){
    if(block[1].position[1] <= (Dimensions.get("window").height/3*2+Dimensions.get("window").height/3)
      && block[1].position[1] >= (Dimensions.get("window").height/3*2+Dimensions.get("window").height/5-Dimensions.get("window").height/3)){

      block[0].score += 10
      score = block[0].score

    }else{
      if(block[0].score>0){
        block[0].score -= 5
        score = block[0].score
      }
    }
  }
  if(touch[2] === true){
    if(block[2].position[1] <= (Dimensions.get("window").height/3*2+Dimensions.get("window").height/3)
      && block[2].position[1] >= (Dimensions.get("window").height/3*2+Dimensions.get("window").height/5-Dimensions.get("window").height/3)){

      block[0].score += 10
      score = block[0].score

    }else{
      if(block[0].score>0){
        block[0].score -= 5
        score = block[0].score
      }
    }
  }

  if(touch[3] === true){
    if(block[3].position[1] <= (Dimensions.get("window").height/3*2+Dimensions.get("window").height/3)
      && block[3].position[1] >= (Dimensions.get("window").height/3*2+Dimensions.get("window").height/5-Dimensions.get("window").height/3)){

      block[0].score += 10
      score = block[0].score

    }else{
      if(block[0].score>0){
        block[0].score -= 5
        score = block[0].score
      }
    }
  }
  if(touch[4] === true){

    if(block[4].position[1] <= (Dimensions.get("window").height/3*2+Dimensions.get("window").height/3)
      && block[4].position[1] >= (Dimensions.get("window").height/3*2+Dimensions.get("window").height/5-Dimensions.get("window").height/3)){

      block[0].score += 10
      score = block[0].score
    }else{
      if(block[0].score>0){
        block[0].score -= 5
        score = block[0].score
      }
    }
  }

  return entities;
}
